﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clotho.UI {
	public interface IUiFeature {
		IEnumerable<ControlKeeper> Create_control_table { get; }
	}
	public class ControlKeeper {
		protected StudioControl control { get; set; }
		/// <summary>从字符串创建窗体的方法 </summary>
		protected readonly Func<string, StudioControl> dCreateControl;
		public readonly string Control_type;
		public object Tag { get; set; }
		public readonly List<(string, EventHandler)> cmd_table;
		public event EventHandler eHeadTextChanged;
		string head_string;
		/// <summary>获取或设置标题文字 </summary>
		public string Head_text {
			get => head_string; set {
				head_string = value;
				eHeadTextChanged?.Invoke(this,EventArgs.Empty);
				if (control != null) {
					MutFormStudio.Control_expand.setHeadText(control, value);
				}
			}
		}

		/// <summary>获取控件创建字符串</summary>
		protected readonly Func<string> dGetCreateString;
		public string Create_string => dGetCreateString.Invoke();

		/// <summary>从中取出控件 </summary>
		public Control Control { get {
				if ((control == null) || ((control as Control).IsDisposed)) {
					control = dCreateControl("");
					MutFormStudio.Control_expand.setHeadText(control, Head_text);
					control.dGetCreateString = dGetCreateString;
					control.Head_text = this.Head_text;
					(control as Control).Disposed += (s, e) => this.control = null;
				}
				return control as Control;
			}
		}

		private void ControlKeeper_eHeadTextChanged(object sender, EventArgs e) {
			MutFormStudio.Control_expand.setHeadText(control, Head_text);
		}

		public ControlKeeper(Func<string, StudioControl> dCreateControl, string head_text, Func<string> dGetCreateString, string control_type) {
			this.dCreateControl = dCreateControl;
			Head_text = head_text;
			this.dGetCreateString = dGetCreateString;
			Control_type = control_type;
			cmd_table = new List<(string, EventHandler)>(10);
		}
	}
	public interface StudioControl: MutFormStudio.StudioControl {

		Func<string> dGetCreateString { get; set; }
	}
}
